﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BulldogCMS.Data.ContentTree
{
    public class XMLContentTreeProvider : IContentTreeProvider
    {
        private XDocument _contentXdoc = null;
        private List<IPartialNode> _partialNodes = null;

        public XMLContentTreeProvider(XDocument xDoc)
        {
            this._contentXdoc = xDoc;
        }

        public IEnumerable<IPartialNode> PartialNodes
        {
            get {
                if (this._partialNodes == null)
                {
                    this._partialNodes = this.LoadPartialNodes();
                }
                return this._partialNodes;
            }
        }

        private List<IPartialNode> LoadPartialNodes()
        {
            List<IPartialNode> partialNodes = new List<IPartialNode>();
            if (this._contentXdoc != null && this._contentXdoc.Root != null && this._contentXdoc.Root.Element("nodes") != null)
            {
                foreach (var nodeElement in this._contentXdoc.Root.Element("nodes").Elements())
                {
                    XMLPartialNode partialNode = new XMLPartialNode(nodeElement);
                    partialNodes.Add(partialNode);
                }
            }
            return partialNodes;
        }

    }
}
